<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 */

require_once ROOT_PATH."/lib/dao/DMLFunctions.php";
require_once ROOT_PATH."/lib/dao/SQLQBuilder.php";
require_once ROOT_PATH."/lib/common/CommonFunctions.php";
require_once ROOT_PATH."/lib/models/hrfunct/EmpInfo.php";

class Workgroup {

	const WORKGROUP_TABLE = "hs_hr_workgroup";
	const EMPLOYEE_WORKGROUP_TABLE = "hs_hr_employee_workgroup";
	const DB_FIELD_WORKGROUP_ID = "group_id";
	const DB_FIELD_NAME = "name";
	const DB_FIELD_GROUP_OBJECT = "group_object";
	const DB_FIELD_GROUP_TYPENUM = "group_typenum";
	const DB_FIELD_GROUP_TYPE = "group_type";
	const DB_FIELD_EMP_QUANTITY = "emp_quantity";
	const DB_FIELD_GROUP_LEADER = "group_leader";
	const DB_FIELD_MANAGER = "manager";
	const DB_FIELD_BOSS = "boss";
	const DB_FIELD_EMP_NUMBER = "emp_number";


	private $workgroupId;
	private $name;
	private $group_object;
	private $group_typenum;
	private $group_type;
	private $empQuantity;
	private $employees;
	private $group_leader;
	private $manager;
	private $boss;

	/**
	 * Construct workgroup object
	 */
	public function __constructor() {

	}

	/**
	 * Get workgroup Id
	 */
	public function getWorkgroupId() {
		return $this->workgroupId;
	}

	/**
	 * Set workgroup Id
	 * @param int $workgroupId work shift id
	 */
	public function setWorkgroupId($workgroupId) {
		$this->workgroupId = $workgroupId;
	}

	/**
	 * Get name
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * Sets worksheet name
	 * @param string $name work sheet name
	 */
	public function setName($name) {
		$this->name = $name;
	}

	/**
	 * Get group object
	 */
	public function getGroupObject() {
		return $this->group_object;
	}

	/**
	 * Get group typenum
	 */
	public function getGroupTypeNum() {
		return $this->group_typenum;
	}

	/**
	 * Get group type
	 */
	public function getGroupType() {
		return $this->group_type;
	}

	/**
	 * Sets group type
	 * @param string $group_type group type
	 */
	public function setGroupObject($group_type) {
		switch ($group_type) {
			case "车间1班":
			case "车间2班":		
			case "车间3班":		
			case "车间4班":		
				$group_object=1;
				break;
			case "保安1班":
			case "保安2班":	
			case "保安3班":	
			case "保安4班":	
				$group_object=3;
				break;
			case "厨房1班":
			case "厨房2班":
			case "厨房3班":
			case "厨房4班":
				$group_object=4;
				break;
			case "空白班":
				$group_object=5;
				break;
			default:
				$group_object=2;
		}
		$this->group_object = $group_object;
	}

	/**
	 * Sets group type
	 * @param string $group_type group type
	 */
	public function setGroupTypeNum($group_type) {
		switch ($group_type) {
			case "行政夜班":
			case "车间2班":
			case "保安2班":
			case "厨房2班":
				$group_typenum=2;
				break;
			case "车间3班":
			case "保安3班":
			case "厨房3班":
				$group_typenum=3;
				break;
			case "车间4班":
			case "保安4班":
			case "厨房4班":
				$group_typenum=4;
				break;
			default:
				$group_typenum=1;
		}
		$this->group_typenum = $group_typenum;
	}

	/**
	 * Sets group type
	 * @param string $group_type group type
	 */
	public function setGroupType($group_type) {
		$this->group_type = $group_type;
	}

	/**
	 * Get employee quantity
	 * @return employee quantity
	 */
	public function getEmployeeQuantity() {
		return $this->empQuantity;
	}

	/**
	 * Set employee quantity
	 * @param int $empQuantity employee quantity
	 */
	public function setEmpQuantity($empQuantity) {
		$this->empQuantity = $empQuantity;
	}

	/**
	 * Get group leader
	 * @return group leader
	 */
	public function getGroupLeader() {
		return $this->group_leader;
	}

	/**
	 * Set group leader
	 * @param char $group_leader group leader
	 */
	public function setGroupLeader($group_leader) {
		$this->group_leader = $group_leader;
	}

	/**
	 * Get manager
	 * @return manager
	 */
	public function getManager() {
		return $this->manager;
	}

	/**
	 * Set manager
	 * @param char $manager manager
	 */
	public function setManager($manager) {
		$this->manager = $manager;
	}

	/**
	 * Get boss
	 * @return boss
	 */
	public function getBoss() {
		return $this->boss;
	}

	/**
	 * Set boss
	 * @param char $boss boss
	 */
	public function setBoss($boss) {
		$this->boss = $boss;
	}

	/**
	 * Get employees
	 * @return employees
	 */
	public function getEmployees() {
		return $this->employees;
	}

	/**
	 * Set employees
	 * @return employees
	 */
	public function setEmployees($employees) {
		$this->employees = $employees;
	}

	/**
	 * Save this workgroup
	 * @return int number of rows changed (1 if update was done, 0 if not)
	 */
	public function save() {
		if (empty($this->empQuantity) || empty($this->name) || $this->empQuantity <= 0) {
			throw new WorkgroupException("Values not set", WorkgroupException::VALUES_EMPTY_OR_NOT_SET);
		}

		if (empty($this->workgroupId)) {
			return $this->_insert();
		} else {
			return $this->_update();
		}
	}

	private function _insert() {

		$fields[0] = self::DB_FIELD_WORKGROUP_ID;
		$fields[1] = self::DB_FIELD_NAME;
		$fields[2] = self::DB_FIELD_GROUP_TYPE;
		$fields[3] = self::DB_FIELD_GROUP_LEADER;
		$fields[4] = self::DB_FIELD_MANAGER;
		$fields[5] = self::DB_FIELD_BOSS;
		$fields[6] = self::DB_FIELD_EMP_QUANTITY;
		$fields[7] = self::DB_FIELD_GROUP_OBJECT;
		$fields[8] = self::DB_FIELD_GROUP_TYPENUM;

		$this->workgroupId = UniqueIDGenerator::getInstance()->getNextID(self::WORKGROUP_TABLE, self::DB_FIELD_WORKGROUP_ID);
		$values[0] = $this->workgroupId;
		$values[1] = "'{$this->name}'";
		$values[2] = "'{$this->group_type}'";
		$values[3] = "'{$this->group_leader}'";
		$values[4] = "'{$this->manager}'";
		$values[5] = "'{$this->boss}'";
		$values[6] = "'{$this->empQuantity}'";
		$values[7] = "'{$this->group_object}'";
		$values[8] = "'{$this->group_typenum}'";

		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::WORKGROUP_TABLE;
		$sqlBuilder->flg_insert = 'true';
		$sqlBuilder->arr_insert = $values;
		$sqlBuilder->arr_insertfield = $fields;

		$sql = $sqlBuilder->addNewRecordFeature2();

		$dbConnection = new DMLFunctions();
		$result = $dbConnection->executeQuery($sql);
		if (!$result) {
			throw new WorkgroupException("Workgroup not inserted", WorkgroupException::ERROR_IN_DB_QUERY);
		}

		if (mysql_affected_rows() != 1) {
			throw new WorkgroupException("Workgroup not inserted", WorkgroupException::INVALID_ROW_COUNT);
		}

		return mysql_affected_rows();
	}

	private function _update() {

		$updateTable = self::WORKGROUP_TABLE;

		$fields[0] = self::DB_FIELD_NAME;
		$fields[1] = self::DB_FIELD_GROUP_TYPE;
		$fields[2] = self::DB_FIELD_GROUP_LEADER;
		$fields[3] = self::DB_FIELD_MANAGER;
		$fields[4] = self::DB_FIELD_BOSS;
		$fields[5] = self::DB_FIELD_EMP_QUANTITY;
		$fields[6] = self::DB_FIELD_GROUP_OBJECT ;
		$fields[7] = self::DB_FIELD_GROUP_TYPENUM ;

		$updateValues[0] = "'" . $this->name . "'";
		$updateValues[1] = $this->group_type;
		$updateValues[2] = $this->group_leader;
		$updateValues[3] = $this->manager;
		$updateValues[4] = $this->boss;
		$updateValues[5] = $this->empQuantity;
		$updateValues[6] = $this->group_object;
		$updateValues[7] = $this->group_typenum;

		$updateConditions[0] = self::DB_FIELD_WORKGROUP_ID . " = " . $this->workgroupId;

		$sqlBuilder = new SQLQBuilder();
		$query = $sqlBuilder->simpleUpdate($updateTable, $fields, $updateValues, $updateConditions);
		$dbConnection = new DMLFunctions();

		$result = $dbConnection->executeQuery($query);
		if ($result === false) {
			throw new WorkgroupException("Error in update", WorkgroupException::ERROR_IN_DB_QUERY);
		}

		return mysql_affected_rows();
	}

	/**
	 * Delete this Workgroup
	 */
	public function delete() {

		if (!CommonFunctions::isValidId($this->workgroupId)) {
			throw new WorkgroupException("Invalid id", WorkgroupException::INVALID_ID);
		}

		$arrList = array($this->workgroupId);
		$count1 = self::_deleteGroupMembers($arrList);
		$count2 = self::_deleteWorkgroups($arrList);

		if (mysql_affected_rows() !== 1) {
			throw new WorkgroupException("Error in Delete", WorkgroupException::INVALID_ROW_COUNT);
		}

	}

	private static function _deleteWorkgroups($ids) {

		$tableName = self::WORKGROUP_TABLE;
		$arrFieldList[0] = self::DB_FIELD_WORKGROUP_ID;

		$sqlBuilder = new SQLQBuilder();

		$sqlBuilder->table_name = $tableName;
		$sqlBuilder->flg_delete = 'true';
		$sqlBuilder->arr_delete = $arrFieldList;

		$arrList[] = $ids;
		$sqlQString = $sqlBuilder->deleteRecord($arrList);

		$dbConnection = new DMLFunctions();
		$result = $dbConnection->executeQuery($sqlQString);

		if ($result) {
			return mysql_affected_rows();
		} else {
			throw new WorkgroupException("Error in SQL Query", WorkgroupException::ERROR_IN_DB_QUERY);
		}
	}

	private static function _deleteGroupMembers($ids) {

		$tableName = self::EMPLOYEE_WORKGROUP_TABLE;
		$arrFieldList[0] = self::DB_FIELD_WORKGROUP_ID;

		$sqlBuilder = new SQLQBuilder();

		$sqlBuilder->table_name = $tableName;
		$sqlBuilder->flg_delete = 'true';
		$sqlBuilder->arr_delete = $arrFieldList;

		$arrList[] = $ids;
		$sqlQString = $sqlBuilder->deleteRecord($arrList);

		$dbConnection = new DMLFunctions();
		$result = $dbConnection->executeQuery($sqlQString);

		if ($result) {
			return mysql_affected_rows();
		} else {
			throw new WorkgroupException("Error in SQL Query", WorkgroupException::ERROR_IN_DB_QUERY);
		}
	}

	/**
	 * Assign employees to this workgroup
	 * @param array $employeeIds Array of employee ids to assign
	 */
	public function assignEmployees($empNumbers) {

		if (!CommonFunctions::isValidId($this->workgroupId)) {
			throw new WorkgroupException("Invalid workgroup id: ". $this->workgroupId, WorkgroupException::INVALID_ID);
		}

		// Filter out non-valid employee numbers
		$empNumbers = array_filter($empNumbers, array("CommonFunctions", "isValidId"));

		if (count($empNumbers) == 0) {
			return 0;
		}

		$empNumberList = implode(",", $empNumbers);

		$sql = "INSERT IGNORE INTO " . self::EMPLOYEE_WORKGROUP_TABLE . "(" . self::DB_FIELD_WORKGROUP_ID . ", " . self::DB_FIELD_EMP_NUMBER .") " .
		       "SELECT b." . self::DB_FIELD_WORKGROUP_ID . ", a." . EmpInfo::EMPLOYEE_FIELD_EMP_NUMBER . " " .
		       "FROM " . EmpInfo::EMPLOYEE_TABLE_NAME . " a, " . self::WORKGROUP_TABLE . " b " .
		       "WHERE a." . EmpInfo::EMPLOYEE_FIELD_EMP_NUMBER . " IN (" . $empNumberList . ") AND " .
		       " b." . self::DB_FIELD_WORKGROUP_ID . " = " . $this->workgroupId;

		$conn = new DMLFunctions();
		$results = $conn->executeQuery($sql);

		if ($results === false) {
			throw new WorkgroupException("Error in db query:" . $sql . ": " . mysql_error(), WorkgroupException::ERROR_IN_DB_QUERY);
		}

		return mysql_affected_rows();
	}

	/**
	 * Get workgroup for given employee
	 *
	 * @param int $empNumber The employee number
	 * @return Workgroup Workgroup for employee or null if no workgroup assigned
	 */
	public static function getWorkgroupForEmployee($empNumber) {

		if (!CommonFunctions::isValidId($empNumber)) {
			throw new WorkgroupException("Invalid emp number: $empNumber", WorkgroupException::INVALID_ID);
		}

		$fields[0] = "a.`" . self::DB_FIELD_WORKGROUP_ID . "`";
		$fields[1] = "a.`" . self::DB_FIELD_NAME . "`";
		$fields[2] = "a.`" . self::DB_FIELD_EMP_QUANTITY . "`";

		$tables[0] = "`" . self::WORKGROUP_TABLE . "` a ";
		$tables[1] = "`" . self::EMPLOYEE_WORKGROUP_TABLE . "` b ";

		$joinConditions[1] = "a." . self::DB_FIELD_WORKGROUP_ID .
							 " = b." . self::DB_FIELD_WORKGROUP_ID;

		$selectConditions[0] = " b." . self::DB_FIELD_EMP_NUMBER . " = " . $empNumber;

		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->selectFromMultipleTable($fields, $tables, $joinConditions, $selectConditions);

		$conn = new DMLFunctions();
		$results = $conn->executeQuery($sql);

		if ($results === false) {
			throw new WorkgroupException("Error in db query:" . $sql, WorkgroupException::ERROR_IN_DB_QUERY);
		}

		$numResults = mysql_num_rows($results);
		if ($numResults == 1) {
			$objs = self::_getWorkgroupsFromResults($results);
			return $objs[0];
		} else if ($numResults == 0) {
			return null;
		} else {
			throw new WorkgroupException("Invalid number of results returned.", WorkgroupException::INVALID_ROW_COUNT);
		}
	}

	/**
	 * Gets workgroup hours for all employees
	 *
	 * @return array empid->workgroup array for all employees with workgroups.
	 */
	public static function getWorkgroupForAllEmployees() {

		$fields[0] = "b.`" . self::DB_FIELD_EMP_NUMBER . "`";
		$fields[1] = "a.`" . self::DB_FIELD_EMP_QUANTITY . "`";

		$tables[0] = "`" . self::WORKGROUP_TABLE . "` a ";
		$tables[1] = "`" . self::EMPLOYEE_WORKGROUP_TABLE . "` b ";

		$joinConditions[1] = "a." . self::DB_FIELD_WORKGROUP_ID .
							 " = b." . self::DB_FIELD_WORKGROUP_ID;

		$selectConditions = null;

		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->selectFromMultipleTable($fields, $tables, $joinConditions, $selectConditions);

		$conn = new DMLFunctions();
		$results = $conn->executeQuery($sql);

		if ($results === false) {
			throw new WorkgroupException("Error in db query:" . $sql, WorkgroupException::ERROR_IN_DB_QUERY);
		}

		$numResults = mysql_num_rows($results);
		$empWorkgroups = array();

		while($row = mysql_fetch_array($results)) {

			$empId = $row[self::DB_FIELD_EMP_NUMBER];
			$hours = $row[self::DB_FIELD_EMP_QUANTITY];
			$empWorkgroups[$empId] = $hours;
		}

		return $empWorkgroups;
	}

	/**
	 * Remove all employees assigned to this workgroup
	 * @return int Number of employees removed from workgroup
	 */
	public function removeAssignedEmployees() {

		if (!CommonFunctions::isValidId($this->workgroupId)) {
			throw new WorkgroupException("Invalid id", WorkgroupException::INVALID_ID);
		}

		$sql = sprintf("DELETE FROM %s WHERE %s = %s", self::EMPLOYEE_WORKGROUP_TABLE,
		                self::DB_FIELD_WORKGROUP_ID, $this->workgroupId);
		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);
		if ($result) {
			$count = mysql_affected_rows();
		} else {
			throw new WorkgroupException("Error in SQL Query: $sql", WorkgroupException::ERROR_IN_DB_QUERY);
		}

		return $count;
	}

	/**
	 * Return list of employees who are not assigned to any workgroup
	 * @return array Array of employees
	 */
	public static function getEmployeesWithoutWorkgroup() {

		$fields[0] = EmpInfo::EMPLOYEE_FIELD_EMP_NUMBER;
		$fields[1] = EmpInfo::EMPLOYEE_FIELD_EMP_ID;
		$fields[2] = EmpInfo::EMPLOYEE_FIELD_FIRST_NAME;
		$fields[3] = EmpInfo::EMPLOYEE_FIELD_MIDDLE_NAME;
		$fields[4] = EmpInfo::EMPLOYEE_FIELD_LAST_NAME;

		$sqlBuilder = new SQLQBuilder();

		$sqlBuilder->table_name = EmpInfo::EMPLOYEE_TABLE_NAME;
		$sqlBuilder->flg_select = 'true';
		$sqlBuilder->arr_select = $fields;
		$sqlBuilder->field = EmpInfo::EMPLOYEE_FIELD_EMP_NUMBER;
		$sqlBuilder->field2 = self::DB_FIELD_EMP_NUMBER;
		$sqlBuilder->table2_name = self::EMPLOYEE_WORKGROUP_TABLE;

		//$sql = $sqlBuilder->selectFilter('','',2);
		$sql = $sqlBuilder->selectFilter();

		$connection = new DMLFunctions();
		$result = $connection->executeQuery($sql);
		if ($result === false) {
			throw new WorkgroupException("Error in db query:" . $sql, WorkgroupException::ERROR_IN_DB_QUERY);
		}

		return self::_getEmployeesFromResults($result);

	}

	/**
	 * Return list of employees who are assigned to this workgroup
	 * @return array Array of employees
	 */
	public function getAssignedEmployees() {

		if(!CommonFunctions::isValidId($this->workgroupId)) {
			throw new WorkgroupException("No valid workgroup id defined", WorkgroupException::INVALID_ID);
		}

		$fields[0] = "b.`" . EmpInfo::EMPLOYEE_FIELD_EMP_NUMBER . "`";
		$fields[1] = "b.`" . EmpInfo::EMPLOYEE_FIELD_EMP_ID . "`";
		$fields[2] = "b.`" . EmpInfo::EMPLOYEE_FIELD_FIRST_NAME . "`";
		$fields[3] = "b.`" . EmpInfo::EMPLOYEE_FIELD_MIDDLE_NAME . "`";
		$fields[4] = "b.`" . EmpInfo::EMPLOYEE_FIELD_LAST_NAME . "`";

		$tables[0] = "`" . self::EMPLOYEE_WORKGROUP_TABLE. "` a ";
		$tables[1] = "`" . EmpInfo::EMPLOYEE_TABLE_NAME . "` b ";

		$joinConditions[1] = "a." . self::DB_FIELD_EMP_NUMBER .
							 " = b." . EmpInfo::EMPLOYEE_FIELD_EMP_NUMBER;

		$selectConditions[0] = " a." . self::DB_FIELD_WORKGROUP_ID . " = " . $this->workgroupId;

		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->selectFromMultipleTable($fields, $tables, $joinConditions, $selectConditions);

		$conn = new DMLFunctions();
		$results = $conn->executeQuery($sql);

		if ($results === false) {
			throw new WorkgroupException("Error in db query:" . $sql, WorkgroupException::ERROR_IN_DB_QUERY);
		}

		return self::_getEmployeesFromResults($results);
	}

	/**
	 * Get work shifts defined in the system
	 * @return array array of work shift objects
	 */
	public static function getWorkgroups() {

		$selectFields[0] = self::DB_FIELD_WORKGROUP_ID;
		$selectFields[1] = self::DB_FIELD_NAME;
		$selectFields[2] = self::DB_FIELD_GROUP_TYPE;
		$selectFields[3] = self::DB_FIELD_GROUP_LEADER;
		$selectFields[4] = self::DB_FIELD_MANAGER;
		$selectFields[5] = self::DB_FIELD_BOSS;
		$selectFields[6] = self::DB_FIELD_EMP_QUANTITY;

		$sqlBuilder = new SQLQBuilder();
		$query = $sqlBuilder->simpleSelect(self::WORKGROUP_TABLE, $selectFields);

		$dbConnection = new DMLFunctions();
		$result = $dbConnection->executeQuery($query);

		if ($result === false) {
			throw new WorkgroupException("Error in db query:" . $query, WorkgroupException::ERROR_IN_DB_QUERY);
		}

		$workgroups = self::_getWorkgroupsFromResults($result);
		return $workgroups;
	}

	/**
	 * Get work shift with given id
	 * @param int $workgroupId Work shift id
	 * @return Workgroup workgroup object or null if not found
	 */
	public static function getWorkgroup($workgroupId) {

		if (!CommonFunctions::isValidId($workgroupId)) {
			throw new WorkgroupException("Invalid id", WorkgroupException::INVALID_ID);
		}

		$sqlBuilder = new SQLQBuilder();

		$selectTable = self::WORKGROUP_TABLE;

		$selectFields[0] = self::DB_FIELD_WORKGROUP_ID;
		$selectFields[1] = self::DB_FIELD_NAME;
		$selectFields[2] = self::DB_FIELD_GROUP_TYPE;
		$selectFields[3] = self::DB_FIELD_GROUP_LEADER;
		$selectFields[4] = self::DB_FIELD_MANAGER;
		$selectFields[5] = self::DB_FIELD_BOSS;
		$selectFields[6] = self::DB_FIELD_EMP_QUANTITY;

		$selectConditions[0] = self::DB_FIELD_WORKGROUP_ID . " = " . $workgroupId;

		$query = $sqlBuilder->simpleSelect($selectTable, $selectFields, $selectConditions);

		$dbConnection = new DMLFunctions();
		$result = $dbConnection->executeQuery($query);

		$numResults = mysql_num_rows($result);
		if ($numResults == 1) {
			$objs = self::_getWorkgroupsFromResults($result);
			return $objs[0];
		} else if ($numResults == 0) {
			throw new WorkgroupException("Invalid number of results returned.", WorkgroupException::WORKGROUP_NOT_FOUND);
		} else {
			throw new WorkgroupException("Invalid number of results returned.", WorkgroupException::INVALID_ROW_COUNT);
		}
	}

	/**
	 * Return array of workgroup objects from the given results set
	 * @return array Array of Workgroup objects
	 */
	private function _getWorkgroupsFromResults($results) {

		$workgroupArray = array();
		while($row = mysql_fetch_array($results)) {

			$workgroupObj = new Workgroup();
			$workgroupObj->setWorkgroupId($row[self::DB_FIELD_WORKGROUP_ID]);
			$workgroupObj->setName($row[self::DB_FIELD_NAME]);
			$workgroupObj->setGroupType($row[self::DB_FIELD_GROUP_TYPE]);
			$workgroupObj->setGroupObject($row[self::DB_FIELD_GROUP_TYPE]);
			$workgroupObj->setGroupTypeNum($row[self::DB_FIELD_GROUP_TYPE]);
			$workgroupObj->setGroupLeader($row[self::DB_FIELD_GROUP_LEADER]);
			$workgroupObj->setManager($row[self::DB_FIELD_MANAGER]);
			$workgroupObj->setBoss($row[self::DB_FIELD_BOSS]);
			$workgroupObj->setEmpQuantity($row[self::DB_FIELD_EMP_QUANTITY]);

			$workgroupArray[] = $workgroupObj;
		}

		return $workgroupArray;
	}

	/**
	 * Return array of workgroup objects from the given results set
	 * @return array Array of Employees from the results set
	 */
	private static function _getEmployeesFromResults($results) {

		$employees = array();
		while($row = mysql_fetch_array($results)) {
			$employees[] = $row;
		}

		return $employees;

	}

	/**
	 * Delete workgroups
	 * @param array $workgroupIds array of work sheet id's to delete
	 */
	public static function deleteWorkgroups($workgroupIds) {

		if (!is_array($workgroupIds) || empty($workgroupIds)) {
			throw new WorkgroupException("Invalid Parameter", WorkgroupException::INVALID_PARAMETER);
		}

		foreach($workgroupIds as $id) {
			if (!CommonFunctions::isValidId($id)) {
				throw new WorkgroupException("Invalid ID in array", WorkgroupException::INVALID_ID);
			}
		}

		self::_deleteWorkgroups($workgroupIds);
	}
}

class WorkgroupException extends Exception {

	const ERROR_IN_DB_QUERY = 1;
	const INVALID_PARAMETER = 2;
	const INVALID_ID= 3;
	const VALUES_EMPTY_OR_NOT_SET = 4;
	const INVALID_ROW_COUNT = 5;
	const WORKGROUP_NOT_FOUND = 6;

}
?>
